package com.jt.www.admin.order_pc.service.impl;

import com.jt.www.admin.order_pc.service.OrgMarketerService;
import com.jt.www.dao.mapper.orgmarketer.OrgMarketerMapper;
import com.jt.www.domain.enums.common.IsOrNotEnum;
import com.jt.www.domain.po.OrgMarketer;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Date;
import java.util.List;

/**
 * Created by shaoyn on 2020/07/28.
 */
@Service
public class OrgMarketerServiceImpl implements OrgMarketerService {

    public static final Logger log = LoggerFactory.getLogger(OrgMarketerServiceImpl.class);
    @Autowired
    private OrgMarketerMapper orgMarketerMapper;


    @Override
    public int save(OrgMarketer orgMarketer) {
        log.info("组织userCde:{} 添加关联营销员! 操作人: {},{}", orgMarketer.getOrgUserCode(), orgMarketer.getOperatorNo(), orgMarketer.getOperatorName());
        orgMarketer.setUpdateTime(new Date());
        orgMarketer.setCreateTime(new Date());
        orgMarketer.setIsDeleted(IsOrNotEnum.NO.getValue());
        return orgMarketerMapper.insertSelective(orgMarketer);
    }

    @Override
    public int delete(OrgMarketer orgMarketer) {
        log.info("组织userCde:{} 关联营销员关系移除! 操作人: {},{}", orgMarketer.getOrgUserCode(), orgMarketer.getOperatorNo(), orgMarketer.getOperatorName());
        OrgMarketer update = new OrgMarketer();
        update.setOperatorNo(orgMarketer.getOperatorNo());
        update.setOperatorName(orgMarketer.getOperatorName());
        update.setIsDeleted(IsOrNotEnum.IS.getValue());
        update.setUpdateTime(new Date());

        Weekend<OrgMarketer> weekend = Weekend.of(OrgMarketer.class);
        WeekendCriteria<OrgMarketer, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(OrgMarketer::getIsDeleted, IsOrNotEnum.NO.getValue());
        criteria.andEqualTo(OrgMarketer::getOrgUserCode, orgMarketer.getOrgUserCode());

        return orgMarketerMapper.updateByExampleSelective(update, weekend);
    }


    @Override
    public OrgMarketer selectByOrgName(String orgName) {
        OrgMarketer orgMarketer = new OrgMarketer();
        orgMarketer.setOrgName(orgName);
        orgMarketer.setIsDeleted(IsOrNotEnum.NO.getValue());
        List<OrgMarketer> orgMarketers = orgMarketerMapper.select(orgMarketer);
        if (CollectionUtils.isEmpty(orgMarketers)) {
            return null;
        }
        //返回第一条记录
        return orgMarketers.get(0);
    }

    @Override
    public OrgMarketer selectByOrgCode(String orgCode) {
        OrgMarketer orgMarketer = new OrgMarketer();
        orgMarketer.setOrgUserCode(orgCode);
        orgMarketer.setIsDeleted(IsOrNotEnum.NO.getValue());
        List<OrgMarketer> orgMarketers = orgMarketerMapper.select(orgMarketer);
        if (CollectionUtils.isEmpty(orgMarketers)) {
            return null;
        }
        //返回第一条记录
        return orgMarketers.get(0);
    }

    @Override
    public List<OrgMarketer> selectByOrgUserCodes(List<String> orgUserCodes) {
        Weekend<OrgMarketer> weekend = Weekend.of(OrgMarketer.class);
        WeekendCriteria<OrgMarketer, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(OrgMarketer::getIsDeleted, IsOrNotEnum.NO.getValue());
        criteria.andIn(OrgMarketer::getOrgUserCode, orgUserCodes);
        return orgMarketerMapper.selectByExample(weekend);
    }
}
